//
//  VEThroughCarViewController.m
//  VEMoneyPorject
//
//  Created by BlueWind on 2018/9/6.
//  Copyright © 2018年 陈建. All rights reserved.
//

#import "VEThroughCarViewController.h"
#import "VETFlashTableViewCell.h"
#import "DTLogInViewController.h"

@interface VEThroughCarViewController ()


@property (nonatomic, strong)VETFlashTableViewCell *cell;

@end

@implementation VEThroughCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    

    
    
    // 可以刷新可以加载更多
    self.refreshType = CJBaseTableVcRefreshTypeRefreshAndLoadMore;
    [self cj_refresh];
}


- (void)cj_refresh{
    [super cj_refresh];
    [self loadData];
}

- (void)loadData{
    [super loadData];
    [self requestDataWithType:1];
    
}

- (void)cj_loadMore{
    [super cj_loadMore];
    NSLog(@"%ld",self.page);
    [self requestDataWithType:2];
    
}

- (void)requestDataWithType:(NSInteger)type{
    
    NSString *url = [NSString stringWithFormat:@"%@/VEService/GetKuaiXun.do",kPublicInterfaceAPI];
    CJBaseRequest *request = [CJBaseRequest cj_requestWithUrl:url isPost:YES withParameters:@{@"Type":@"2", @"Page":@(self.page)}];
    [request cj_sendRequestWithCompletion:^(id response, BOOL success, NSString *message) {
        [self cj_endRefresh];
        [self cj_endLoadMore];
        self.isSuccessNet = YES;
        NSArray *arr = response;
        if (arr.count == 0) {
            self.page -=1;
            return;
        }
        if (type == 1) {
            [self.dataArray removeAllObjects];
            self.dataArray = [VEFastModel mj_objectArrayWithKeyValuesArray:arr];
        } else {
            NSArray *arr1 = [VEFastModel mj_objectArrayWithKeyValuesArray:arr];
            [self.dataArray addObjectsFromArray:arr1];
        }
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)cj_numberOfSections {
    return 1;
}

/**
 某个cell的数量
 */
- (NSInteger)cj_numberOfRowsInsection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)cj_cellHeightAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat h = [_cell getHeightWihtIndexPath:indexPath];
    
    return h;
}
- (CGFloat)cj_sectionFooterHeightAtSection:(NSInteger)section {
    return 0.0001;
}

/**
 某行的cell
 */
- (CJBaseTableViewCell *)cj_cellAtIndexPath:(NSIndexPath *)indexPath {
    
    _cell= [VETFlashTableViewCell cellWithTableView:self.tableView];
    _cell.fastModel = self.dataArray[indexPath.row];
    VEFastModel *model = self.dataArray[indexPath.row];
    WEAKSELF
    _cell.shareFast = ^{
        
        if (![[CJUserInfoManager sharedManager] isLogin]) {
            DTLogInViewController *vc = [[DTLogInViewController alloc] init];
            vc.loginSuccessBlock = ^{
            };
            [weakSelf pushVc:vc];
            
        } else {
            NSString *VECODE = [[CJUserInfoManager sharedManager] currentUserInfo].VECODE;
            NSString *str = [NSString stringWithFormat:@"%@/WebForm1.aspx?vecode=%@",kPublicInterfaceAPI,VECODE];
            
            [[shareFunctionCJ shareFunction] shareInfoP:weakSelf.view viewController:weakSelf WithUrlSTr:str WithTitle:@"分享快讯" withImageViewUrlStr:nil withDetailTitleString:nil withType:0];
            
            [shareFunctionCJ shareFunction].ShareWXSuccessBlock = ^{
                NSString *userId = [[CJUserInfoManager sharedManager] currentUserInfo].USERID;
                
                if (![NSObject isNilOrNull:userId]) {
                    NSString *url = [NSString stringWithFormat:@"%@/VEService/AddCoinRecord.do",kPublicInterfaceAPI];
                    
                    NSDictionary *dic = @{
                                          @"Userid": userId,
                                          @"Articleid": model.ARTICLEID,
                                          @"Cointype": @"分享直通车",
                                          @"Coinnumber": @"50"
                                          };
                    
                    CJBaseRequest *request = [CJBaseRequest cj_requestWithUrl:url isPost:YES withParameters:dic];
                    [request cj_sendRequestWithCompletion:^(id response, BOOL success, NSString *message) {
                        if ([message isEqualToString:@"保存成功！"]) {
                            [CJUIKit showWithString:@"分享直通车奖励+50"];
                        }else if ([message isEqualToString:@"只有第一次阅读和分享奖励糖果！"]){
                            
                        }else{
                            [CJUIKit showWithString:message];
                        }
                    }];
                    
                }
            };
            
        }
    };
    return _cell;
    
}

- (void)cj_didSelectCellAtIndexPath:(NSIndexPath *)indexPath cell:(CJBaseTableViewCell *)cell {
    
    VEFastModel *model = self.dataArray[indexPath.row];
    
    if (model.isSpread == NO) {
        model.isSpread = YES;
    } else {
        model.isSpread = NO;
    }
    [UIView performWithoutAnimation:^{
        //        [self.tableView cj_reloadSingleRowAtIndexPath:indexPath animation:None];
        [self.tableView reloadData];
    }];
    
}





@end
